package org.gjy.m8.tool;

import com.jd.platform.async.executor.Async;
import com.jd.platform.async.wrapper.WorkerWrapper;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-09-07 14:57:50
 */
public class AsyncTool2Test {

    private static final Logger log = LoggerFactory.getLogger(AsyncTool2Test.class);

    @Test
    public void mixed4() throws ExecutionException, InterruptedException {
        Work0 work0 = new Work0();
        Work1 work1 = new Work1();
        Work2 work2 = new Work2();
        Work3 work3 = new Work3();
        Work4 work4 = new Work4();

        WorkerWrapper<String, String> wrapper4 = new WorkerWrapper.Builder<String, String>()
                .worker(work4).callback(work4).param("work4").build();

        WorkerWrapper<String, String> wrapper1 = new WorkerWrapper.Builder<String, String>()
                .worker(work1).callback(work1).next(wrapper4, false).param("work1").build();
        WorkerWrapper<String, String> wrapper2 = new WorkerWrapper.Builder<String, String>()
                .worker(work2).callback(work2).next(wrapper4, false).param("work2").build();
        WorkerWrapper<String, String> wrapper3 = new WorkerWrapper.Builder<String, String>()
                .worker(work3).callback(work3).next(wrapper4, false).param("work3").build();

        WorkerWrapper<String, String> wrapper0 = new WorkerWrapper.Builder<String, String>()
                .worker(work0).callback(work0).next(wrapper1, wrapper2, wrapper3).param("work0").build();

        /*WorkerWrapper<String, String> wrapper1 = new WorkerWrapper.Builder<String, String>()
                .worker(work1).callback(work1).depend(wrapper0, false).next(wrapper4).param("work1").build();
        WorkerWrapper<String, String> wrapper2 = new WorkerWrapper.Builder<String, String>()
                .worker(work2).callback(work2).depend(wrapper0, false).next(wrapper4).param("work2").build();
        WorkerWrapper<String, String> wrapper3 = new WorkerWrapper.Builder<String, String>()
                .worker(work3).callback(work3).depend(wrapper0, false).next(wrapper4).param("work3").build();*/


        Async.beginWork(3000, wrapper0);
        Async.shutDown();
    }

    @Test
    public void mixed3() throws ExecutionException, InterruptedException {
        Work0 work0 = new Work0();
        Work1 work1 = new Work1();
        Work2 work2 = new Work2();
        Work3 work3 = new Work3();
        Work4 work4 = new Work4();

        WorkerWrapper<String, String> wrapper0 = new WorkerWrapper.Builder<String, String>()
                .worker(work0).callback(work0).param("work0").build();
        WorkerWrapper<String, String> wrapper1 = new WorkerWrapper.Builder<String, String>()
                .worker(work1).callback(work1).param("work1").build();

        WorkerWrapper<String, String> wrapper2 = new WorkerWrapper.Builder<String, String>()
                .worker(work2).callback(work2).depend(wrapper0).param("work2").build();
        WorkerWrapper<String, String> wrapper3 = new WorkerWrapper.Builder<String, String>()
                .worker(work3).callback(work3).depend(wrapper1).param("work3").build();
        WorkerWrapper<String, String> wrapper4 = new WorkerWrapper.Builder<String, String>()
                .worker(work4).callback(work4).depend(wrapper3).next(wrapper2, false).param("work4").build();

        Async.beginWork(5000, Executors.newCachedThreadPool(), wrapper0, wrapper1);
        Async.shutDown();
    }

    @Test
    public void mixed2() throws ExecutionException, InterruptedException {
        Work0 work0 = new Work0();
        Work1 work1 = new Work1();
        Work2 work2 = new Work2();
        Work3 work3 = new Work3();

        WorkerWrapper<String, String> wrapper0 = new WorkerWrapper.Builder<String, String>()
                .worker(work0).callback(work0).param("work0").build();
        WorkerWrapper<String, String> wrapper3 = new WorkerWrapper.Builder<String, String>()
                .worker(work3).callback(work3).param("work3").build();
        WorkerWrapper<String, String> wrapper2 = new WorkerWrapper.Builder<String, String>()
                .worker(work2).callback(work2)
                .next(wrapper3).depend(wrapper0).param("work2").build();
        WorkerWrapper<String, String> wrapper1 = new WorkerWrapper.Builder<String, String>()
                .worker(work1).callback(work1)
                .next(wrapper3).depend(wrapper0).param("work1").build();

        log.info("执行任务开始");
        Async.beginWork(6000, wrapper0);
        log.info("任务执行结束");
        Async.shutDown();
    }

    @Test
    public void mixed1() throws ExecutionException, InterruptedException {
        Work0 work0 = new Work0();
        Work1 work1 = new Work1();
        Work2 work2 = new Work2();
        Work3 work3 = new Work3();

        WorkerWrapper<String, String> wrapper3 = new WorkerWrapper.Builder<String, String>()
                .worker(work3).callback(work3).param("work03").build();

        WorkerWrapper<String, String> wrapper2 = new WorkerWrapper.Builder<String, String>()
                .worker(work2).callback(work2).param("work2").next(wrapper3).build();

        WorkerWrapper<String, String> wrapper1 = new WorkerWrapper.Builder<String, String>()
                .worker(work1).callback(work1).param("work1").next(wrapper3).build();

        WorkerWrapper<String, String> wrapper0 = new WorkerWrapper.Builder<String, String>()
                .worker(work0).callback(work0).param("work0").next(wrapper1, wrapper2).build();

        log.info("执行任务开始");
        Async.beginWork(6000, wrapper0);
        log.info("任务执行结束");
        Async.shutDown();
    }

    @Test
    public void parallel() throws ExecutionException, InterruptedException {
        Work0 work0 = new Work0();
        Work1 work1 = new Work1();
        Work2 work2 = new Work2();

        WorkerWrapper<String, String> wrapper2 = new WorkerWrapper.Builder<String, String>()
                .worker(work2)
                .callback(work2)
                .param("work2")
                .build();
        WorkerWrapper<String, String> wrapper1 = new WorkerWrapper.Builder<String, String>()
                .worker(work1)
                .callback(work1)
                .param("work1")
                .build();
        WorkerWrapper<String, String> wrapper0 = new WorkerWrapper.Builder<String, String>()
                .worker(work0)
                .callback(work0)
                .param("work0")
                .build();

        log.info("执行任务开始");
        Async.beginWork(1500, wrapper0, wrapper2, wrapper1);
        log.info("任务执行结束");
        Async.shutDown();
    }

    @Test
    public void seq() throws ExecutionException, InterruptedException {
        Work0 work0 = new Work0();
        Work1 work1 = new Work1();
        Work2 work2 = new Work2();

        // 执行顺序 0-1-2
        WorkerWrapper<String, String> wrapper2 = new WorkerWrapper.Builder<String, String>()
                .worker(work2)
                .callback(work2)
                .param("work2")
                .build();
        WorkerWrapper<String, String> wrapper1 = new WorkerWrapper.Builder<String, String>()
                .worker(work1)
                .callback(work1)
                .param("work1")
                .next(wrapper2)
                .build();
        WorkerWrapper<String, String> wrapper0 = new WorkerWrapper.Builder<String, String>()
                .worker(work0)
                .callback(work0)
                .param("work0")
                .next(wrapper1)
                .build();

        log.info("执行任务开始");
        Async.beginWork(1500, wrapper0);
        log.info("任务执行结束");
        Async.shutDown();
    }
}
